using System;
using Newtonsoft.Json.Linq;


namespace _3DTicTacToe.Net
{
    sealed class NetMessage
    {
        public readonly DateTime Timestamp;
        public readonly string Header;

        private readonly JObject _content;

        public JObject Content
        {
            get { return _content; }
        }

        public string Serialize()
        {
            var obj = new JObject {{"Timestamp", Timestamp}, {"Header", Header}, {"Content", Content}};
            return obj.ToString();
        }

        public static NetMessage Deserialize(string data)
        {
            var obj = JObject.Parse(data);
            var header = obj["Header"].Value<string>();
            var timestamp = obj["Timestamp"].Value<DateTime>();
            var content = obj["Content"].Value<JObject>();

            return new NetMessage(header, timestamp, content);
        }

        public override string ToString()
        {
            return Serialize();
        }

        public NetMessage(string header, JObject content)
        {
            Header = header;
            Timestamp = DateTime.Now;
            _content = content;
        }

        private NetMessage(string header, DateTime timestamp, JObject content)
        {
            Header = header;
            Timestamp = timestamp;
            _content = content;
        }
    }
}